﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Drawing.Printing;
using Mani.Common.Security;

namespace Niazi.CRM.Web
{
    public partial class settings : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SecurityManager secManager = new SecurityManager();
            secManager.authProvider = new WebAuthorizationProvider();

            if (!secManager.IsActionPermitted("settings"))
            {
                Response.Redirect("~/access_denied.aspx?action_name=settings");
            }

            if (!Page.IsPostBack)
                LoadPrinters();
        }

        private void LoadPrinters()
        {
            foreach (string strPrinter in PrinterSettings.InstalledPrinters)
            {
                ddlPrinters.Items.Add(strPrinter);
            }

            var item = ddlPrinters.Items.FindByValue(ConfigurationManager.AppSettings["PrinterName"]);

            if (item != null)
                item.Selected = true;
        }

        protected void SaveChanges(object sender, EventArgs e)
        {
            Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
            config.AppSettings.Settings["PrinterName"].Value = ddlPrinters.SelectedValue;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}